<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<title>Document</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	
	<link rel="stylesheet" type="text/css" href="../../css/mui.min.css"/>
	<style type="text/css">
		.list-icon{
			width: 25px;
			height: 25px;
			margin-top: 10px;
			float: left;
		}
		.list-title{
			font-size: 14px;
			display: inline-block;
			/* position: fixed; */
			float: left;
			margin-top: 14px;
			margin-left: 12px;
		}
		
		.mui-grid-view.mui-grid-9 .mui-table-view-cell {
			margin: 0px;
			padding: 5px 10px;
			vertical-align: top;
			border-right: 0px solid #eee;
			border-bottom: 0px solid #eee;
			background-color: white;
		}
		
		.mui-table-view-cell > a:not(.mui-btn).mui-active {
			background-color: #fff;
		}
		
		.mui-table-view.mui-grid-view .mui-table-view-cell .mui-media-body {
			font-size: 13px;
			line-height: 15px;
			display: block;
			width: 100%;
			height: 15px;
			margin-top: 8px;
			text-overflow: ellipsis;
			color: #666;
		}
		
		.mui-table-view:before{
			height: 0;
		}
		
	</style>
</head>
<body>
	
<div class="mui-content">
	<div id="user-baseinfo-id"  v-cloak>
		<div v-if = "!login" id="user-login-id" class="mui-navigate-right" style="height: 120px;background-color: white;margin-bottom: 8px;">
			<div style="padding-top: 40px;padding-left: 20px;display: inline-block;float: left;">
				<img src="../../images/default_sdk_login@2x.png" width="60px" height="60px" >
			</div>
			
			<div style="float: left;margin-top: 60px;margin-left: 15px; display: inline-block; font-size: 15px; color: #888888;">
				请前往登录
			</div>
			
			<div style="float: right;margin-top: 60px;margin-right: 15px;">
				<img src="../../images/arrow_icon@2x.png" width="20px" height="20px" >
			</div>
			
		</div>
		
		<div v-else style="height: 180px;border-width: 0px; border-color: brown;border-style: solid; margin-bottom: 5px;border-radius: 5px;background-color: white;">
			<div onclick="tableDidSelected(0)" style="margin-top: 30px;margin-left: 15px;font-size: 20px; display: inline-block;">
				{{user_name}}
				<div style="font-size: 15px;color: darkgray;padding-top: 10px;">
					查看或编辑资料
				</div>
			</div>
			
			<img :src="avatar" style="width: 60px;height: 60px; border-radius: 50%; float: right;margin-top: 20px;margin-right: 30px;" >
		
			<ul class="mui-table-view mui-grid-view mui-grid-9" style="margin-top: 20px;background-color: white;" >
				<li class="mui-table-view-cell mui-media mui-col-xs-4" style="background-color: white;">
					<a >
						<div class="mui-media-body" style="color: #FF4500;font-size: 16px;">{{item.zanCnt}}</div>
						<div class="mui-media-body">收到的赞</div>
					</a>
				</li> 
				
				<li class="mui-table-view-cell mui-media mui-col-xs-4" style="background-color: white;">
					<a >
						<div class="mui-media-body" style="color: #FF4500;font-size: 16px;">{{item.fanCnt}}</div>
						<div class="mui-media-body">粉丝</div>
					</a>
				</li> 
				
				<li class="mui-table-view-cell mui-media mui-col-xs-4" style="background-color: white;">
					<a >
						<div class="mui-media-body" style="color: #FF4500;font-size: 16px;">{{item.score}}</div>
						<div class="mui-media-body">积分</div>
					</a>
				</li> 
			</ul>
		
		</div>
	</div>
	
	<ul id="me-tableview-id" class="mui-table-view" style="margin-bottom: 20px;">
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(1)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon1.png" class="list-icon">
					<div class="list-title">我的主页</div>
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(2)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon2.png" class="list-icon">
					<div class="list-title">消息</div>
					<span id="new-msgnumber-id" class="mui-badge mui-badge-danger" style="display: none; padding: 2px 8px;"></span>
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(3)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon3.png" class="list-icon">
					<div class="list-title">收藏</div>
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(4)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon4.png" class="list-icon">
					<div class="list-title">粉丝关注</div>
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(5)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon5.png" class="list-icon">
					<div class="list-title">分享给好友</div>
				</a>
			</li>
			<li class="mui-table-view-cell">
				<a onclick="tableDidSelected(6)" class="mui-navigate-right">
					<img src="../../images/me/me_list_icon6.png" class="list-icon">
					<div class="list-title">设置</div>
				</a>
			</li>
		</ul>
</div>

<script src="../../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/p/open.page.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/p/api.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/p/user.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/p/vue.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/p/hud.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	var _userinfo;
	var isfromloginsuccess = false;
	
	//刷新资料
	document.addEventListener('update-profile-success' , function(event){
		isfromloginsuccess = true;
		getUserProfile();
	})
	
	//退出登录
	document.addEventListener('logout-event' , function(event){
		baseinfo.login = false;
		location.reload();
	})
	
	var baseinfo = new Vue({
		el:'#user-baseinfo-id',
		data:{
			item:{},
			login:userHasLogined()
		},
		computed:{
			user_name:function(){
				var user = this.item;
				var uname = user["name"] || user['nickName'];
				return uname;
			},
			avatar:function(){
				var d = this.item;
				var _avatarUrl = d['avatar_thumb'];
				// var u_avatar = getUserAvatarUrl(_avatarUrl , "../../images/default_avatar.png");
				var u_avatar = _avatarUrl || "../../images/default_avatar.png";
				return u_avatar;
			},
			
			isLogined:function(){
				this.login = userHasLogined();
				// return userHasLogined();
			}
		},
		methods:{
			islogined:function(){
				this.login = userHasLogined();
			}
		}
	})
	
	var shares=null;
	
	mui.init()
	mui.plusReady(function () {
		var wv = plus.webview.currentWebview();
		//获取分享服务
		plus.share.getServices(function(s){//
			shares={};
			for(var i in s){
				var t=s[i];
				shares[t.id]=t;
			}
		}, function(e){
			outSet('获取分享服务列表失败：'+e.message);
		});
		
		//检测版本更新
		checkUpdate();
		
		if(userHasLogined()){
			getUserProfile();
			getNewMsgNumber();
		}else{
			document.getElementById('user-login-id').addEventListener('tap',function(){
				if(userHasLogined()){return;}
				openNewPage('../login/login.html' , '手机号登录' , true);	
			})
		}
	
	
	})

	function checkUpdate(){//0:Android 1:iOS
		api_post(check_version_url , {'type':0} , function(res){
			var newVersion = res['version_number'];
			var appid = plus.runtime.appid;
			if(appid != 'HBuilder'){
				plus.runtime.getProperty(appid , function(info){
					var appver =  info['version'];	
					if (newVersion > appver) {
						mui.confirm(res['version_log'],'新版本' ,  ["下载更新", "取消"], function(e) {
						    if(e.index == 0){
						        window.location.href = "http://dancheng0394.com/a/dctt.apk";
						    }
						});
					}			
				});
			}
		})
	}

	var pages = ['me-baseinfo.html'];
	function tableDidSelected(row){
		if(row == 1 || row == 2 || row ==3 || row == 4){if(!userHasLogined()){hud_toast('请先登录');return;}}
		switch (row){
			case 0:openNewPage(pages[0] , '修改资料' , true , {'userinfo':_userinfo || ''});break;
			case 1:openNewPage('me-homepage.html' , _userinfo['name']|| _userinfo['nickName'] , true , {'uid':_userinfo['user_id']});break;
			case 2:
			var msgNum = mui('#new-msgnumber-id')[0];
			msgNum.style.display = 'none';
			openNewPage('me-message.html' , '消息' , true);break;	
			case 3:openNewPage('me-collection.html' , '收藏' , true);break;	
			case 4:openNewPage('me-fans.html' , '粉丝关注' , false);break;	
			case 5:shareShow();break;
			case 6:openNewPage('me-setter.html' , '设置' , true);break;
			default:break;
		}
	}
	
	
	// 打开分享
	function shareShow(){
		var shareBts=[];
		// 更新分享列表
		var ss=shares['weixin'];
		ss&&ss.nativeClient&&(shareBts.push({title:'微信朋友圈',s:ss,x:'WXSceneTimeline'}),
		shareBts.push({title:'微信好友',s:ss,x:'WXSceneSession'}));
		
		ss=shares['qq'];
		ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});
		// 弹出分享列表
		shareBts.length>0?plus.nativeUI.actionSheet({title:'分享到',cancel:'取消',buttons:shareBts}, function(e){
			(e.index>0)&&shareAction(shareBts[e.index-1],true);
		}):plus.nativeUI.alert('当前环境无法支持分享操作!');
	}
	
	/**
	   * 分享操作
	   * @param {JSON} sb 分享操作对象s.s为分享通道对象(plus.share.ShareService)
	   * @param {Boolean} bh 是否分享链接
	   */
	function shareAction(sb,bh) {
		if(!sb||!sb.s){console.log('无效的分享服务！');return;}
		var msg={content:'专注本地生活信息服务，关注分享身边新动态，赶快告诉身边的小伙伴吧!',extra:{scene:sb.x}};
		if(bh){
			msg.href= 'http://dancheng0394.com/a/d/downloadapk.html';
			msg.title='郸城人自己的手中APP';
			msg.thumbs=["../../images/new/app_logo60.png"];
			msg.pictures=["../../images/new/app_logo60.png"];
		}
		
		// 发送分享
		if(sb.s.authenticated){
			console.log('---已授权---');
			shareMessage(msg, sb.s);
		}else{
			console.log('---未授权---');
			sb.s.authorize(function(){
				shareMessage(msg,sb.s);
			}, function(e){
				console.log('认证授权失败：'+e.code+' - '+e.message);
			});
		}
	}
	
	/**
	   * 发送分享消息
	   * @param {JSON} msg
	   * @param {plus.share.ShareService} s
	   */
	function shareMessage(msg, s){
		s.send(msg, function(){
			console.log('分享到"'+s.description+'"成功！');
			if(userHasLogined()){
				var d = {"uid":getLoginUid(), "type":"1","score":add_score_share};
				api_post(update_profile_url , d , function(res){
					hud_toast('分享成功，积分+' + add_score_share);
					getUserProfile();
				} );
			}else{
				hud_toast('分享成功，感谢你的支持和关注!');
			}

		}, function(e){
			hud_close('分享到"'+s.description+'"失败: '+JSON.stringify(e));
			console.log('分享到"'+s.description+'"失败: '+JSON.stringify(e));
		});
	}
	
	
	//用户信息
	function getUserProfile(){
		var uinfo = {"uid":getLoginUid(), "type":"3"};
		api_post(update_profile_url , uinfo , function(res){
			//console.log(JSON.stringify(res));
			if(isfromloginsuccess){
				isfromloginsuccess = false;
				location.reload();
			}
			
			_userinfo = res;
			baseinfo.islogined();
			baseinfo.item = res;
		});
	}
	

	//获取最新消息数
	function getNewMsgNumber(){
		var lastTime = localStorage.getItem('lastTimeGetMessageList');
		var d = {"uid": getLoginUid() , "type":0};//'lastTime':'2019-01-17 14:53:55'
		if(lastTime){d['lastTime'] = lastTime;}
		api_post(get_msglist_url , d , function(res){
			var num = Number(res['num']);
			var shNum = res['sh'];
			//console.log(JSON.stringify(res));
			if(num > 0){
				var msgNum = mui('#new-msgnumber-id')[0];
				msgNum.style.display = 'inline-block';
				msgNum.innerText = num;
			}
			
			if (res['can']) {
				var _l = document.getElementById('me-tableview-id');
				var li = document.createElement('li');
				li.className = 'mui-table-view-cell';
				li.innerHTML = "<a onclick='tosh();' class='mui-navigate-right'><img src='../../images/me/me_list_icon7.png' class='list-icon'><div class='list-title'>审核</div><span id='new-sh-id' class='mui-badge mui-badge-danger' style='display: none; padding: 5px 8px;'>有新动态</span></a>";_l.appendChild(li);
				if (shNum > 0) {
					var _sh = mui('#new-sh-id')[0];
					_sh.style.display = 'inline-block';
					
					///提示
					mui.alert('有新的动态待审核!','提示','确定');
				}
				
			}
		})
	}

	function tosh(){//隐藏'新动态'提示,下次启动应用还是正常显示
		var _sh = mui('#new-sh-id')[0];
		_sh.style.display = 'none';
		openNewPage('me-shenhe.html','待审核',true);
	}
</script>
</body>
</html>